package com.cgt.web.dao.impl;

import com.cgt.web.dao.IClassDao;
import com.cgt.web.pojo.Classroom;
import com.cgt.web.util.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ClassDaoImpl implements IClassDao {

    @Override
    public List<Classroom> selectByPage(int offset, int pageSize) {
        System.out.println("ClassDaoImpl.selectByPage");
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Classroom> list = new ArrayList<>();

        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,name,address,seatsum FROM classroom LIMIT ?,?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,offset);
            statement.setInt(2,pageSize);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while(resultSet.next()){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String address = resultSet.getString("address");
                int seatsum = resultSet.getInt("seatsum");
                Classroom classroom = new Classroom(id,name,address,seatsum);
                list.add(classroom);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }   finally {
            JDBCUtil.close(connection,statement,resultSet);
        }
        return list;
    }

    @Override
    public int selectTotalCount() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int totalcount = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT count(*) FROM classroom";
            statement =  connection.prepareStatement(sql);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while(resultSet.next()){
                totalcount = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }   finally {
            JDBCUtil.close(connection,statement,resultSet);
        }
        return totalcount;
    }

    @Override
    public void deleteById(int id) {
        System.out.println("ClassDaoImpl.deleteById");
        Connection connection = null;
        PreparedStatement statement = null;

        try {
            connection = JDBCUtil.getConnection();
            String sql = "DELETE FROM classroom WHERE id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println(count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }   finally {
            JDBCUtil.close(connection,statement,null);
        }
    }

    @Override
    public void addInf(Classroom classroom) {
        System.out.println("ClassDaoImpl.addInf");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "INSERT INTO classroom(name,address,seatsum) VALUES(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, classroom.getName());
            statement.setString(2, classroom.getAddress());
            statement.setInt(3,classroom.getSeatsum());
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println(count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }   finally {
            JDBCUtil.close(connection,statement,null);
        }
    }

    @Override
    public Classroom selectById(int id) {
        System.out.println("ClassDaoImpl.selectById");
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Classroom classroom = null;

        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT name,address,seatsum FROM classroom WHERE id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                String name = resultSet.getString("name");
                String address = resultSet.getString("address");
                int seatsum = resultSet.getInt("seatsum");
                classroom = new Classroom(id,name,address,seatsum);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }   finally {
            JDBCUtil.close(connection,statement,resultSet);
        }
        return classroom;
    }

    @Override
    public void updateInf(Classroom classroom) {
        System.out.println("ClassDaoImpl.updateInf");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "UPDATE classroom SET name=?,address=?,seatsum=? WHERE id=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, classroom.getName());
            statement.setString(2, classroom.getAddress());
            statement.setInt(3, classroom.getSeatsum());
            statement.setInt(4, classroom.getId());
            System.out.println("ClassDaoImpl.updateInf");
            int count = statement.executeUpdate();
            System.out.println(count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtil.close(connection,statement,null);
        }
    }
}
